package com.chance.cc.crawler.development.command.meta.keyword.baidu;

import com.chance.cc.crawler.development.command.meta.keyword.CrawlerDomainKeywordCommand;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lt
 * @version 1.0
 * @date 2021-04-23 14:10:29
 * @email okprog@sina.com
 */
public class BaiDuKeyword {

    private static final String domain = "baidu";
    private static final String metaSite = "bmwKw";
    private static final String metaBarSite = "bmwBarKw";
    private static CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
            new CrawlerDomainKeywordCommand("192.168.1.215",9599);

    @Test
    public void bmwTest(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String filePath = "C:\\Users\\Administrator\\Documents\\chance\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\baidu\\bmw-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(metaSite);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }
    @Test
    public void bmwBarTest(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String filePath = "C:\\Users\\Administrator\\Documents\\chance\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\baidu\\bmw-bar-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(metaBarSite);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    public static List<String> getContent(String filepath)  {
        //读取文件
        List<String> lineLists = null;
        try {
            System.out.println(filepath);
            lineLists = Files
                    .lines(Paths.get(filepath), Charset.defaultCharset())
                    .flatMap(line -> Arrays.stream(line.split("\n")))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  lineLists;
    }
}
